<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;销售管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;销售订单管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="salesForm"
            :rules="rules"
            ref="salesForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-form-item label="下单日期:" prop="orderDate">
            <el-date-picker
                v-model="salesForm.orderDate"
                type="date"
                placeholder="选择下单日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="订单编号:" prop="orderId">
            <el-input v-model="salesForm.orderId" placeholder="请输入订单编号"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品:" prop="category">
                <el-input v-model="salesForm.category" placeholder="输入企业名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单位:" prop="unit">
                <el-input v-model="salesForm.unit" placeholder="输入单位"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="数量:" prop="count">
                <el-input v-model="salesForm.count" placeholder="输入数量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="单价:" prop="unitPrice">
                <el-input v-model="salesForm.unitPrice" placeholder="输入单价"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="9">
              <el-form-item label="金额:" prop="amount">
                <el-input v-model="salesForm.amount" placeholder="输入金额"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item style="float: right">
                <el-button type="primary"@click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="客户姓名:" prop="customName">
            <el-input v-model="salesForm.customName" placeholder="输入客户姓名"></el-input>
          </el-form-item>
          <el-form-item label="电话号码:" prop="cell">
            <el-input v-model="salesForm.cell" placeholder="输入电话号码"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="送货日期" prop="deliveryDate">
                <el-date-picker
                    v-model="salesForm.deliveryDate"
                    type="date"
                    placeholder="选择送货日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="送货编号:" prop="deliveryOdd">
                <el-input v-model="salesForm.deliveryOdd" placeholder="输入送货编号"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="是否收到:" prop="deliveryCheck">
            <el-input v-model="salesForm.deliveryCheck" placeholder="是否收到"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('salesForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('PayForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      salesForm: {
        orderDate:'2022-03-05',
        orderId: 'Ds123312',
        category: '药品',
        unit:'箱(60盒)',
        count:60,
        unitPrice:200,
        amount:0,
        customName: '阿金',
        cell:'16544981045',
        deliveryDate:'2022-03-12',
        deliveryOdd: 'DIHS1773264',
        deliveryCheck: '是',
      },
      rules: {
        orderDate: [
          {required:true,message:"请选择下单日期",trigger:'change'}
        ],
        orderId:[
          {required: true, message: "请输入订单编号", trigger: 'blur'},
          {pattern: /^[a-zA-Z0-9]+$/, message: "请输入英文数字", trigger: 'blur'}
        ],
        category: [
          {required: true, message: "请输入商品名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        unit: [
          {required: true, message: "请输入单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        count: [
          {required: true, message: "请输入数量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        unitPrice: [
          {required: true, message: "请输入单价", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        amount: [
          {required:true,message:"请输入金额",trigger:'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        customName: [
          {required: true, message: "请输入客户姓名", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        cell: [
          {required: true, message: "请输入电话号码", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        deliveryDate: [
          {required: true, message: "请选择送货日期", trigger: 'change'},
        ],
        deliveryOdd: [
          {required: true, message: "请输入送货编号", trigger: 'blur'},
          {pattern: /^[a-zA-Z0-9]+$/, message: "请输入英文数字", trigger: 'blur'}
        ],
        deliveryCheck: [
          {required: true, message: "是否收到", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    compute(){
      let amount = parseInt(this.salesForm.count) * parseInt(this.salesForm.unitPrice);
      this.$set(this.salesForm,'amount',amount);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/customSales/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.salesForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  },

}

</script>


<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}
</style>